package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //1.用实体类接收参数用来接收前端实体类里面没有时间
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //2.添加数据先将套餐数据加入套餐表然后处理套餐菜品关联的信息
        setmealMapper.save(setmeal);
        //3.添加完套餐数据将数据库里面id进行返回用于菜品进行绑定
        Long id = setmeal.getId();
        //4.获取套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //5.进行判断如果套餐里面没菜品就没必要添加菜品
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(f -> {
                f.setSetmealId(id);
            });
        }
        //6.拿到套餐关联数据然后进行添加
        setmealDishMapper.save(setmealDishes);
    }


    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> setmealPage = setmealMapper.pageQuer(setmealPageQueryDTO);

        PageResult pageResult = new PageResult(setmealPage.getTotal(), setmealPage.getResult());

        return pageResult;
    }


    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //1.首先我们要知道什么不能删除
        //1.1第一正在起售的套餐不能删除
        //1.2这里我们这么知道套餐是否正在起售还是停售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            //1.3这里我们通过查询拿到套餐id根据id获取到套餐数据信息
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //3.第三如果以上都没有就可以直接删除
        for (Long id : ids) {
            setmealMapper.delete(id);
            setmealDishMapper.delete(id);
        }

    }


    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        Long categoryId = setmeal.getCategoryId();

        Category category = categoryMapper.selectBy(categoryId);
        String name = category.getName();

        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealDish(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setCategoryName(name);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.upate(setmeal);

        Long id = setmeal.getId();

        setmealDishMapper.delete(id);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(f -> {
            f.setSetmealId(id);
        });
        setmealDishMapper.save(setmealDishes);
    }


    @Override
    public void updateById(Integer status, Long id) {
        setmealMapper.upateById(status, id);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}



